
package fileby.ctrl;

import fileby.core.ActionKey;
import com.google.inject.Inject;
import com.smardec.mousegestures.MouseGestures;
import com.smardec.mousegestures.MouseGesturesListener;
import fileby.ctrl.actions.IActionProvider;
import fileby.util.properties.IPropertyBundleProvider;
import fileby.util.properties.PropertyBundle;
import java.util.HashMap;

/**
 *
 * @author Ludwig Kjellstrom
 */

public class MouseGesturesHandler implements MouseGesturesListener{

    private HashMap<String,ActionKey> actions = new HashMap<String, ActionKey>();
    private IActionProvider actionProvider;
    private IPropertyBundleProvider propertyBundle;

    @Inject
    public MouseGesturesHandler(IActionProvider actionProvider,
            IPropertyBundleProvider propertyBundle) {

        this.propertyBundle = propertyBundle;
        this.actionProvider = actionProvider;
        initGestures();
        MouseGestures m = new MouseGestures();
        m.addMouseGesturesListener(this);
        m.start();
    }

    private void initGestures() {
        PropertyBundle bundle = propertyBundle.create(this.getClass());

        for(ActionKey key : ActionKey.values())
            addAction(bundle.getString(key.toString()),key);
    }

    private void addAction(String s, ActionKey k){
        //If s is empty or doesn't contain valid characters it's not a valid mousegesture.
        if(s == null || s.isEmpty() || ( !s.contains("L") && !s.contains("R")
                && !s.contains("U") && !s.contains("D") ))
            return;
        actions.put(s, k);
    }

    public void processGesture(String string) {
        if(!string.isEmpty() && actions.containsKey(string)) {
            if(actionProvider.getControl(actions.get(string)).isEnabled())
                actionProvider.getControl(actions.get(string)).actionPerformed(null);
        }
        
    }

    public void gestureMovementRecognized(String string) {
        // do nothing
    }
    
}
